package ru.susu.algebra.partition;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import ru.susu.algebra.util.StringUtilities;


/**
 * @author akargapolov
 * @since: 18.03.2009
 */
public abstract class PartitionBase implements Partition
{
	private static final long serialVersionUID = 4993620050182232629L;

	@Override
	public Partition clone()
	{
		return null;
	}

	@Override
	public boolean add(Integer number)
	{
		return this.add(number, 1);
	}

	@Override
	public boolean remove(Integer number)
	{
		return this.remove(number, 1);
	}

	@Override
	public int compareTo(Partition partition)
	{
		int sumCmp = this.getPresentedNumber().compareTo(partition.getPresentedNumber());
		if (sumCmp != 0)
			return sumCmp;

		return this.getOrderNumber() - partition.getOrderNumber();
	}

	@Override
	public void writeTo(Writer writer, int width) throws IOException
	{
		for (Integer value : this.getArrayRepresentation())
			writer.write(StringUtilities.toString(value, width));
	}

	@Override
	public String toString()
	{
		try
		{
			StringWriter writer = new StringWriter();
			this.writeTo(writer, 3);
			return writer.toString();
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return null;
		}
	}

}
